// pages/users/order/index.js
const {
  voluntaryTemplatePage,
  voluntaryTemplateDelete,
  voluntaryTemplateUse
} = require('../../../api/ai')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    pageNo: 1,
    pageSize: 20,
    total:0,
    loading:true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getList()
  },
  async getList() {
    const {
      pageNo,
      pageSize
    } = this.data
    const params = {
      pageNo,
      pageSize
    }
    const {
      result
    } = await voluntaryTemplatePage(params)
    wx.hideLoading()
    if (result.records.length == 0) return
    this.setData({
      ['list[' + (pageNo - 1) + ']']: result.records,
      pageNo: pageNo + 1,
      total:result.total
    })
  },
  onRefresh() {
    this.setData({
      list: [],
      pageNo: 1
    })
    this.getList()
  },
  async use(e) {
    const {
      id,
      value,
      use
    } = e.currentTarget.dataset
    if (use == 0) {
      const {
        result
      } = await voluntaryTemplateUse(id)
      this.onRefresh()
    }
    const {
      firstSelect,
      secondSelect,
      provinceId,
      province,
      score
    } = value
    const newSecondSelect = secondSelect.replaceAll(',', '')
    const params = {
      firstSelect,
      secondSelect: newSecondSelect,
      provinceId,
      province,
      score,
      id
    }
    wx.navigateTo({
      url: '/pages/volunteer/recommend/index?tempNo=' + value.tempNo + '&params=' + encodeURIComponent(JSON.stringify(params)),
    })

  },
  async delete(e) {
    const {
      id,
      index,
      pindex
    } = e.currentTarget.dataset
    const {
      list
    } = this.data
    const result = await voluntaryTemplateDelete(id)
    // if (result.success){
    //   list[pindex].splice(index,1)
    //   this.setData({
    //     list
    //   })
    // }
    this.onRefresh()
  },
  detail(e) {
    const {
      id
    } = e.currentTarget.dataset
    // wx.navigateTo({
    //   url: '/pages/volunteer/recommend/index?id='+id,
    // })
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    const {
      list,
      pageSize
    } = this.data
    const count = list.length - 1
    if (list[count].length < pageSize) {
      return
    }
    this.getList()
  },
})